package game.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.entity.PlayingRole;
import game.session.SessionManager;
import lion.netty4.message.GamePlayer;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lion.netty4.processor.MsgProcessor;
import lion.netty4.proto.RpcBaseProto.S2CDummy;
import lua.LuaMessageLogin.S2ErrorCode;

public abstract class PlayingRoleMsgProcessor extends MsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(PlayingRoleMsgProcessor.class);

	@Override
	public void process(GamePlayer session, RequestMessageRaw request) throws Exception {
		Long sessionId = session.getSessionId();
		if (sessionId == null) {
			return;
		}
		// 更新最新的session
		PlayingRole savedHero = SessionManager.getInstance().visit(sessionId);
		// 检查session是否过期
		if (savedHero == null) {
			S2ErrorCode respMsg = new S2ErrorCode(-1);
			session.sendMsg(respMsg.build(session.alloc()));
			return;
		}
		process(savedHero, request);
	}

	@Override
	public void process(GamePlayer session, RequestProtoMessage request) throws Exception {
		Long sessionId = session.getSessionId();
		if (sessionId == null) {
			session.writeAndFlush(GameConstants.ERROR_CODE_MSG,
					S2CDummy.newBuilder().setErrorCode(GameConstants.ERROR_CODE_NUM_LOGIN_1).setErrorMessage("请重新登录！")
							.build());
			return;
		}
		// 更新最新的session
		PlayingRole savedHero = SessionManager.getInstance().visit(sessionId);
		// 检查session是否过期
		if (savedHero == null) {
			session.writeAndFlush(GameConstants.ERROR_CODE_MSG,
					S2CDummy.newBuilder().setErrorCode(GameConstants.ERROR_CODE_NUM_LOGIN_1).setErrorMessage("请重新登录！")
							.build());
			return;
		}
		processProto(savedHero, request);
	}

	public abstract void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception;

	public abstract void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception;
	
	public abstract void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception;

	@Override
	public void process(GamePlayer session, MyRequestMessage request) throws Exception {
		Long sessionId = session.getSessionId();
		if (sessionId == null) {
			session.writeAndFlush(GameConstants.ERROR_CODE_MSG,
					S2CDummy.newBuilder().setErrorCode(GameConstants.ERROR_CODE_NUM_LOGIN_1).setErrorMessage("请重新登录！")
							.build());
			return;
		}
		// 更新最新的session
		PlayingRole savedHero = SessionManager.getInstance().visit(sessionId);
		// 检查session是否过期
		if (savedHero == null) {
			session.writeAndFlush(GameConstants.ERROR_CODE_MSG,
					S2CDummy.newBuilder().setErrorCode(GameConstants.ERROR_CODE_NUM_LOGIN_1).setErrorMessage("请重新登录！")
							.build());
			return;
		}
		processWebsocket(savedHero, request);
	}

}
